package com.ayoka.widget;

import android.content.Context;
import android.support.v7.widget.AppCompatButton;
import android.util.AttributeSet;
import android.view.MotionEvent;

import com.ayoka.base.AppContext;
import com.ayoka.utils.AudioRecordManager;


public class RecordAudioButton extends AppCompatButton implements AudioRecordManager.IAudioRecordListener {

    private static final String TAG = "RecordAudioView";

    private Context context;
    private IRecordAudioListener recordAudioListener;
    private boolean isCanceled;
    private float downPointY;
    private static final float DEFAULT_SLIDE_HEIGHT_CANCEL = 150;
    private boolean isRecording;


    public RecordAudioButton(Context context) {
        super(context);
        init(context);
    }

    public RecordAudioButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public RecordAudioButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        this.context = context;
        AudioRecordManager.getInstance().setAudioSaveDirectory(AppContext.getAppCacheDir());
        AudioRecordManager.getInstance().setRecordAudioListener(this);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        super.onTouchEvent(event);
        if (recordAudioListener != null) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    setSelected(true);
                    downPointY = event.getY();
                    recordAudioListener.onFingerPress();
                    startRecordAudio();
                    break;
                case MotionEvent.ACTION_UP:
                    getParent().requestDisallowInterceptTouchEvent(false);
                    setSelected(false);
                    onFingerUp();
                    break;
                case MotionEvent.ACTION_MOVE:
                    onFingerMove(event);
                    getParent().requestDisallowInterceptTouchEvent(true);
                    break;
                case MotionEvent.ACTION_CANCEL:
                    isCanceled = true;
                    onFingerUp();
                    break;
                default:

                    break;
            }
        }
        return true;
    }

    /**
     * 手指抬起,可能是取消录制也有可能是录制成功
     */
    private void onFingerUp() {
        if (isRecording) {
            if (isCanceled) {
                isRecording = false;
                AudioRecordManager.getInstance().cancelRecord();
                recordAudioListener.onRecordCancel();
            } else {
                stopRecordAudio();
            }
        }
    }

    private void onFingerMove(MotionEvent event) {
        float currentY = event.getY();
        isCanceled = checkCancel(currentY);
        if (isCanceled) {
            recordAudioListener.onSlideTop();
        } else {
            recordAudioListener.onFingerPress();
        }
    }

    private boolean checkCancel(float currentY) {
        return downPointY - currentY >= DEFAULT_SLIDE_HEIGHT_CANCEL;
    }

    /**
     * 检查是否ready录制,如果已经ready则开始录制
     */
    private void startRecordAudio() throws RuntimeException {
        boolean isPrepare = recordAudioListener.onRecordPrepare();
        AudioRecordManager.getInstance().onPrepare(isPrepare);
        if (isPrepare) {
            //准备就绪开始录制
            try {
                AudioRecordManager.getInstance().startRecord();
                isRecording = true;
            } catch (Exception e) {
                e.printStackTrace();
                this.recordAudioListener.onRecordCancel();
            }
        }
    }

    /**
     * 停止录音
     */
    private void stopRecordAudio() throws RuntimeException {
        if (isRecording) {
            try {
                isRecording = false;
                AudioRecordManager.getInstance().stopRecord();
            } catch (Exception e) {
                e.printStackTrace();
                this.recordAudioListener.onRecordCancel();
            }
        }
    }

    /**
     * 需要设置IRecordAudioStatus,来监听开始录音结束录音等操作,并对权限进行处理
     *
     * @param recordAudioListener
     */
    public void setRecordAudioListener(IRecordAudioListener recordAudioListener) {
        this.recordAudioListener = recordAudioListener;
    }

    public void invokeStop() {
        onFingerUp();
    }

    @Override
    public void onStartRecord() {
        recordAudioListener.onRecordStart();
    }

    @Override
    public void onCompiledRecord(String audioPath, long duration) {
        recordAudioListener.onRecordStop(audioPath, duration);
    }

    @Override
    public void onCancelRecord() {
        recordAudioListener.onRecordCancel();
    }

    @Override
    public void onAudioAmplitudeChanged(float amplitude) {

    }

    public interface IRecordAudioListener {
        boolean onRecordPrepare();

        void onRecordStart();

        boolean onRecordStop(String audioPath, long duration);

        boolean onRecordCancel();

        void onSlideTop();

        void onFingerPress();
    }
}
